package com.zxb.base.task.service;

import cn.hutool.core.collection.CollectionUtil;
import com.zxb.base.task.enm.TaskStateEnum;
import com.zxb.core.dao.system.SysTask;
import com.zxb.core.dao.system.SysTaskDao;
import com.zxb.base.task.MyJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Zxb
 */
@Slf4j
@Service
@AllArgsConstructor
public class QuartzServiceImpl implements QuartzService {

    private Scheduler scheduler;
    private SysTaskDao sysTaskDao;

    @Override
    public void startAll() throws SchedulerException {
        //启动任务管理器
        scheduler.start();
        //只查询运行态的任务
        List<SysTask> list = new ArrayList<>();
        if (CollectionUtil.isEmpty(list)) {
            if (log.isInfoEnabled()) {
                log.info("没有找到定时任务");
            }
            return;
        }
        for (SysTask task : list) {
            addJob(task);
            task.setState(TaskStateEnum.运行.name());
            sysTaskDao.updateById(task);
        }
        if (log.isInfoEnabled()) {
            log.info("所有任务已经启动...");
        }
    }

    @Override
    public void addJob(SysTask task) throws SchedulerException {
        String identity = "trigger-" + task.getName();

        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity(task.getName()).build();
        jobDetail.getJobDataMap().put("task", task);

        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(task.getCron());

        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(identity)
                .forJob(jobDetail).withSchedule(cronScheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    @Override
    public void modifyJob(String code, String cron) throws SchedulerException {
        Date date = null;
        String identity = "trigger-" + code;
        TriggerKey triggerKey = new TriggerKey(identity);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldCron = cronTrigger.getCronExpression();
        if (!oldCron.equalsIgnoreCase(cron)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);

            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(identity)
                    .withSchedule(cronScheduleBuilder.withMisfireHandlingInstructionDoNothing()).build();

            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        System.out.println(date);
    }

    @Override
    public void pauseJob(String code) throws SchedulerException {
        JobKey jobKey = new JobKey(code);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail != null) {
            scheduler.pauseJob(jobKey);
        }
    }

    @Override
    public void resumeJob(String code) throws SchedulerException {
        JobKey jobKey = new JobKey(code);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail != null) {
            scheduler.resumeJob(jobKey);
        }
    }

    @Override
    public void delJob(String code) throws SchedulerException {
        JobKey jobKey = new JobKey(code);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail != null) {
            scheduler.deleteJob(jobKey);
        }
    }

    @Override
    public void doJob(SysTask task) throws SchedulerException {
        System.out.println("=====》"+task.getUri());
    }
}
